package com.yundahh.u8c.dao;

import com.yundahh.u8c.config.U8CConfig;
import com.yundahh.u8c.dao.base.U8CBaseDao;
import com.yundahh.u8c.entity.Dept;
import com.yundahh.u8c.entity.base.U8CResult;
import com.yundahh.u8c.enums.DeptAttr;
import com.yundahh.u8c.enums.OperationType;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class DeptDao extends U8CBaseDao<Dept> {
    public DeptDao(U8CConfig config) {
        super(config);
    }

    @Override
    public String getApiUrl() {
        return "/u8cloud/api/uapbd/bddept/";
    }

    /**
     * 获取采购部门
     * @param unitcode
     * @return
     * @throws IOException
     */
    public List<Dept> getPurchaseDepts(String unitcode) throws IOException {
        U8CResult<Dept> send = send(OperationType.QUERY, "{\"page_now\":\"1\",\"page_size\":\"100\",\"unitcode\":\""+unitcode+"\"}");
        List<Dept> datas = send.getResultData().getDatas();
        List<Dept> collect = datas.stream().filter(e -> e.getDeptattr()== DeptAttr.PURCHASE|| e.getDeptattr()==DeptAttr.PURCHASE_SALES).collect(Collectors.toList());
        return  collect;
    }

    /**
     * 获取销售部门
     * @param unitcode
     * @return
     * @throws IOException
     */
    public List<Dept> getSalesDepts(String unitcode) throws IOException {
        U8CResult<Dept> send = send(OperationType.QUERY, "{\"page_now\":\"1\",\"page_size\":\"100\",\"unitcode\":\""+unitcode+"\"}");
        List<Dept> datas = send.getResultData().getDatas();
        List<Dept> collect = datas.stream().filter(e -> e.getDeptattr()== DeptAttr.SALES|| e.getDeptattr()==DeptAttr.PURCHASE_SALES).collect(Collectors.toList());
        return  collect;
    }
}
